package com.xiaofangjie.excel;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.bioroadlab.capture.StringUtil;

public class GeneCapture {
	
	public static void main(String[] args) {
		try {
			URL url = new URL("http://www.omim.org/entry/100640?search=100640&highlight=100640");
			HttpURLConnection conn = ((HttpURLConnection) url.openConnection());
			InputStreamReader input = new InputStreamReader(conn.getInputStream());
			BufferedReader buff = new BufferedReader(input);
			String str = "";
			while((str=buff.readLine())!=null){
				if(str.contains("HGNC Approved Gene Symbol:")){
					System.out.println(str);
					str = str.substring(str.lastIndexOf("\">")+2, str.length());
					str = StringUtil.getSupString(str.trim());
					captureGene(str);
				}
			}
			
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

	private static void captureGene(String strParameter) {
		try {
			URL url = new URL("http://www.ncbi.nlm.nih.gov/gene?term=" + strParameter);
			HttpURLConnection conn = ((HttpURLConnection) url.openConnection());
			InputStreamReader input = new InputStreamReader(conn.getInputStream());
			BufferedReader buff = new BufferedReader(input);
			String str = "";
			while((str=buff.readLine())!=null){
				if(str.contains("[Homo sapiens]")){
					str = StringUtil.getSupString(str);
					System.out.println(str);
					String fullName = str.substring(str.indexOf("[Homo sapiens]") + "[Homo sapiens]".length(), str.indexOf("Official Symbol:"));
					str = buff.readLine();
					str = StringUtil.getSupString(str);
					
					String officialSymbol = str.substring(0, str.indexOf("Other Aliases"));
					System.out.println("第二个：\t"+officialSymbol);
					
					String os = str.substring(0, str.indexOf("Other Aliases"));
					String oA = str.substring(str.indexOf("Other Aliases:")+"Other Aliases:".length(), str.indexOf("Other Designations:"));
					String oD = str.substring(str.indexOf("Other Designations:")+"Other Designations:".length(),str.indexOf("Location"));
					String location = str.substring(str.indexOf("Location: ")+"Location: ".length(), str.indexOf("Annotation"));
					String mim = str.substring(str.indexOf("MIM: ")+"MIM: ".length(), str.indexOf("ID:"));
					System.out.println("fullName:\t"+fullName.trim());
					System.out.println("oS:\t"+os.trim());
					System.out.println("oA\t"+oA.trim());
					System.out.println("oD\t"+oD.trim());
					System.out.println("location\t"+location.trim());
					System.out.println("mim\t"+mim.trim());
				}
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
